package com.zankong.tool.zkapp.v8fn;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.eclipsesource.v8.JavaCallback;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;
import com.zankong.tool.zkapp.R;
import com.zankong.tool.zkapp.ZKToolApi;
import com.zankong.tool.zkapp.activity.ZKActivity;
import com.zankong.tool.zkapp.util.Util;
import com.zankong.tool.zkapp.util.V8Utils;
import com.zankong.tool.zkapp.util.ZKAppV8;
import com.zankong.tool.zkapp.zk_interface.ZKV8Fn;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by YF on 2018/7/16.
 */

@ZKAppV8
public class ZKNotify implements ZKV8Fn {
    private V8Object NotificationV8;
    private NotificationManager mNotifyMgr;
    private int notifyId = 100;
    public ZKNotify(){
        mNotifyMgr = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationV8 = new V8Object(ZKToolApi.runtime);
    };

    private Context getContext(){
        return ZKToolApi.getInstance().getContext();
    }

    @Override
    public void addV8Fn() {

        NotificationV8.registerJavaMethod(new JavaCallback() {
            @Override
            public Object invoke(V8Object receiver, V8Array parameters) {
                try{
                Notification.Builder builder = new Notification.Builder(getContext())
                        .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                        .setContentTitle("name")
                        .setContentText("subject")
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setAutoCancel(true);
//
                    RemoteViews remoteViews = new RemoteViews(getContext().getPackageName(), R.layout.v8fn_notify_default);
                    Intent intent = new Intent(getContext(), ZKActivity.class);
                    for (String s : parameters.getObject(0).getKeys()) {
                        String value = parameters.getObject(0).getString(s);
                        switch (s) {
                            case "title":
                                remoteViews.setTextViewText(R.id.title,value);
                                break;
                            case "url":
                                intent.putExtra("page",value);
                                break;
                            case "img":
                                remoteViews.setImageViewBitmap(R.id.img,Util.getImageFromAssetsFile(value));
                                break;
                            case "content":
                                remoteViews.setTextViewText(R.id.content,value);
                                break;
                            case "time":
                                remoteViews.setTextViewText(R.id.time,value);
                                break;
                        }
                    }
                    intent.putExtra("arguments", V8Utils.js2string(parameters.getObject(1)));
                    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(),0 ,intent,PendingIntent.FLAG_UPDATE_CURRENT);
                    builder.setContentIntent(pendingIntent);
                    builder.setContent(remoteViews);
                    Notification notification = builder.build();
                    mNotifyMgr.notify(notifyId++,notification);
                }catch (Exception e){
                    e.printStackTrace();
                }

                return null;
            }
        },"notify");
        ZKToolApi.runtime.add("Notification",NotificationV8);
    }
}
